﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;

namespace ImageCombinerCore
{
    public static class ColorBlendHelper
    {
        public static Color Normal(Color x, Color y)
        {
            return Color.FromArgb(1, y.R, y.G, y.B);
        }

        public static Color Light(Color x, Color y)
        {
            var r = Mathf.Max(x.R, y.R);
            var g = Mathf.Max(x.G, y.G);
            var b = Mathf.Max(x.B, y.B);

            return Color.FromArgb(1, r, g, b);
        }

        public static Color Darken(Color x, Color y)
        {
            var r = Mathf.Min(x.R, y.R);
            var g = Mathf.Min(x.G, y.G);
            var b = Mathf.Min(x.B, y.B);

            return Color.FromArgb(1, r, g, b);
        }

        public static Color Overlay(Color x, Color y)
        {
            var r = x.R <= 128 ? x.R * y.R / 128 : ((byte.MaxValue - x.R) * (byte.MaxValue - x.R)) / 128;
            var g = x.G <= 128 ? x.G * y.G / 128 : ((byte.MaxValue - x.G) * (byte.MaxValue - x.G)) / 128;
            var b = x.B <= 128 ? x.B * y.B / 128 : ((byte.MaxValue - x.B) * (byte.MaxValue - x.B)) / 128;

            return Color.FromArgb(1, r, g, b);
        }

        public static Color Transparency(Color background, Color target, float opacity)
        {
            opacity = 1 - opacity;
            var r = (byte)Mathf.Max(0, background.R + (target.R - background.R) * opacity);
            var g = (byte)Mathf.Max(0, background.G + (target.G - background.G) * opacity);
            var b = (byte)Mathf.Max(0, background.B + (target.B - background.B) * opacity);

            return Color.FromArgb(1, r, g, b);
        }
    }
}
